$(() => {
    let getList = () => {
        $.ajax({
            type: 'get',
            url: 'http://localhost:8080/api/v1/admin/category/list',
            headers: {
                Authorization: localStorage.getItem('dsj_token')
            },
            success: (res) => {
                // console.log(res);
                let htmlStr = template('moban', res)
                $('tbody').html(htmlStr)
            }

        })
    }
    getList()
    $('.btn_opt').on('click', function () {
        let name = $('#name').val().trim()
        let slug = $('#slug').val().trim()
        if (name === '' || slug === '') return alert('请输入内容')
        // console.log({ name, slug });
        $.ajax({
            type: 'post',
            url: 'http://localhost:8080/api/v1/admin/category/add',
            data: { name, slug },
            headers: {
                Authorization: localStorage.getItem('dsj_token')
            },
            success: (res) => {
                console.log(res);
                if (res.code === 200) {
                    getList()
                    $('#myModal').modal('hide')
                    $('#name').val('')
                    $('#slug').val('')

                } else {
                    alert(res.msg)
                }
            }

        })
    })

    let id
    $('tbody').on('click', '.btn_bianji', function () {

        $('#myModal2').modal('show')
        id = $(this).data('id')
        $.ajax({
            type: 'get',
            url: 'http://localhost:8080/api/v1/admin/category/search',
            data: { id },
            headers: {
                Authorization: localStorage.getItem('dsj_token')
            },
            success: (res) => {
                // console.log(res);
                $('#name2').val(res.data[0].name)
                $('#slug2').val(res.data[0].slug)
            }
        })
    })


    $('.btn_opt2').on('click', function () {
        let name = $('#name2').val().trim()
        let slug = $('#slug2').val().trim()
        if (name === '' || slug === '') return alert('不能为空')
        $.ajax({
            type: 'post',
            url: 'http://localhost:8080/api/v1/admin/category/edit',
            data: { id, name, slug },
            headers: {
                Authorization: localStorage.getItem('dsj_token')
            },
            success: (res) => {
                if (res.code === 200) {
                    getList()
                    $('#myModal2').modal('hide')
                } else {
                    alert(res.msg)
                }
            }

        })
    })

    $('tbody').on('click', '.btn_del', function () {
        let id = $(this).data('id')
        layer.confirm('是否删除?', { icon: 3, title: '温馨提示' }, function (index) {
            $.ajax({
                type: 'post',
                url: 'http://localhost:8080/api/v1/admin/category/delete',
                data: { id },
                headers: {
                    Authorization: localStorage.getItem('dsj_token')
                },
                success: (res) => {
                    getList()
                    // console.log(res);
                    // alert(res.msg)
                }
            })
            //do something
            layer.close(index);
        });

    })
})